﻿package view.components.utils
{
    import flash.geom.*;

    public class TrigoBase extends Object
    {
        public static const HALF_PI:Number = 1.5708;
        public static const QUARTER_PI:Number = 0.785398;
        public static const DEG_TO_RAD:Number = 0.0174533;
        public static const TWO_PI:Number = 6.28319;
        public static const RAD_TO_DEG:Number = 57.2958;
        public static const THIRD_PI:Number = 1.0472;
        public static const PI:Number = 3.14159;

        public function TrigoBase()
        {
            return;
        }// end function

        public static function getDegree(param1:Point, param2:Point) : Number
        {
            var _loc_3:* = Math.atan2(param2.y - param1.y, param2.x - param1.x);
            return radian2degree(_loc_3);
        }// end function

        public static function degree2radian(param1:Number) : Number
        {
            return resolveDegree(param1) * DEG_TO_RAD;
        }// end function

        public static function getPointOnQuadraticBezierCurve(param1:Point, param2:Point, param3:Point, param4:Number) : Point
        {
            var _loc_5:* = new Point();
            new Point().x = param1.x + param4 * (2 * (1 - param4) * (param3.x - param1.x) + param4 * (param2.x - param1.x));
            _loc_5.y = param1.y + param4 * (2 * (1 - param4) * (param3.y - param1.y) + param4 * (param2.y - param1.y));
            return _loc_5;
        }// end function

        public static function getRadian(param1:Point, param2:Point) : Number
        {
            return degree2radian(getDegree(param1, param2));
        }// end function

        public static function closestPointOnLine(param1:Point, param2:Point, param3:Point) : Point
        {
            var _loc_4:* = findU(param1.x, param1.y, param2.x, param2.y, param3.x, param3.y);
            if (findU(param1.x, param1.y, param2.x, param2.y, param3.x, param3.y) <= 0)
            {
                return param1;
            }
            if (_loc_4 >= 1)
            {
                return param2;
            }
            var _loc_5:* = param1.x + _loc_4 * (param2.x - param1.x);
            var _loc_6:* = param1.y + _loc_4 * (param2.y - param1.y);
            return new Point(_loc_5, _loc_6);
        }// end function

        public static function resolveDegree(param1:Number) : Number
        {
            var _loc_2:* = param1 % 360;
            return _loc_2 < 0 ? (360 + _loc_2) : (_loc_2);
        }// end function

        public static function shortestAngle(param1:Number, param2:Number) : Number
        {
            var _loc_3:* = param2 - param1;
            if (_loc_3 > 180)
            {
                _loc_3 = _loc_3 - 360;
            }
            if (_loc_3 < -180)
            {
                _loc_3 = _loc_3 + 360;
            }
            return _loc_3;
        }// end function

        public static function getDistance(param1:Point, param2:Point) : Number
        {
            var _loc_3:* = param1.x - param2.x;
            var _loc_4:* = param1.y - param2.y;
            return Math.sqrt(_loc_3 * _loc_3 + _loc_4 * _loc_4);
        }// end function

        public static function closestPointOnPoly(param1:Point, param2:Point, param3:Point) : Point
        {
            var _loc_4:Point = null;
            var _loc_5:* = Number.POSITIVE_INFINITY;
            var _loc_6:* = closestPointOnLine(param1, param2, param3);
            var _loc_7:* = TrigoBase.getDistance(_loc_6, param3);
            if (TrigoBase.getDistance(_loc_6, param3) < _loc_5)
            {
                _loc_5 = _loc_7;
                _loc_4 = _loc_6;
            }
            return _loc_4;
        }// end function

        private static function findU(param1:Number, param2:Number, param3:Number, param4:Number, param5:Number, param6:Number) : Number
        {
            var _loc_7:* = (param5 - param1) * (param3 - param1) + (param6 - param2) * (param4 - param2);
            var _loc_8:* = (param3 - param1) * (param3 - param1) + (param4 - param2) * (param4 - param2);
            return _loc_7 / _loc_8;
        }// end function

        public static function radian2degree(param1:Number) : Number
        {
            return resolveDegree(param1 * RAD_TO_DEG);
        }// end function

        public static function polar(param1:Point, param2:Number, param3:Number) : Point
        {
            var _loc_4:* = degree2radian(param3);
            return new Point(param1.x + Math.cos(_loc_4) * param2, param1.y + Math.sin(_loc_4) * param2);
        }// end function

    }
}
